package com.pt.pok.string;

import com.pt.pok.CommandHandler;
import com.pt.pok.CommandListener;
import com.pt.pok.GeneralProxyException;
import com.pt.pok.model.LoginRequest;
import com.pt.pok.model.PokerCommand;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author Roman.Ivanov@playtech.com, Andrey.Panfilov@playtech.com
 * @date 1/23/12 {@inheritDoc} LoginRequest String handler
 */
class LoginRequestCommandHandler
    implements CommandHandler<String> {

    private final List<CommandListener> listeners = new CopyOnWriteArrayList<CommandListener>();

    private void notifyListeners(LoginRequest loginRequest) {
        for (CommandListener listener : listeners) {
            listener.onCommand(loginRequest);
        }
    }

    public void handle(String data)
        throws GeneralProxyException {
        LoginRequest lr = unmarshalRequest(data);
        notifyListeners(lr);
    }

    private LoginRequest unmarshalRequest(String data)
        throws GeneralProxyException {
        LoginRequest lr = new LoginRequest();
        lr.setPassword("");
        lr.setUsername("");
        return lr;
    }

    public PokerCommand handleDirect(String data)
        throws GeneralProxyException {
        return unmarshalRequest(data);
    }

    public void addListener(CommandListener loginRequestCommandListener) {
        listeners.add(loginRequestCommandListener);
    }

    public void removeListener(CommandListener commandListener) {
        listeners.remove(commandListener);
    }
}
